package com.ce.framework.auth;

import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import com.ce.util.DbUtil;
import com.ce.util.RegeditUtil;


/**
 * 嫦娥软件有限公司 版权所有 2013
 * 创建人： andy 
 * 创建时间： 2013-10-2 上午10:55:22
 * 功能描述：  登录验证过滤器
 * ==================================
 * 修改历史
 * 修改人        修改时间      修改位置（函数名）
 *
 * ==================================
 */
public class LogUserOpratorFilter implements Filter
{
    public void init(FilterConfig arg0) throws ServletException {
    	String str = arg0.getInitParameter("interval");
    	try {
			interval = Integer.parseInt(str);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
    }
    
    Pattern pat = Pattern.compile("([^=]+)=[^&]+");

    @SuppressWarnings("unchecked")
	public void doFilter(ServletRequest arg0, 
            ServletResponse arg1, 
            FilterChain arg2) 
            throws IOException, ServletException {
    	if(!isLogInfo()){
            arg2.doFilter(arg0,arg1);
            return;
    	}

        HttpServletRequest request = (HttpServletRequest)arg0;
        String sessionId = request.getSession().getId();
        String method = request.getMethod();
        String queryStr = request.getQueryString();
        String uri = request.getRequestURI();
        if(queryStr!=null&&queryStr.length()>0){
        	uri += "?"+queryStr;
        }
        HashSet<String> set = new HashSet<String>();
        if(queryStr!=null&&queryStr.length()>0){
            Matcher mat = pat.matcher(queryStr);
            while(mat.find()){
            	set.add(mat.group(1));
            }
        }
		JSONObject param = new JSONObject(),header = new JSONObject();
		Enumeration<String> en = request.getParameterNames();
		String pname = null;
		while(en.hasMoreElements()){
			pname = en.nextElement();
			if(set.contains(pname)){
				continue;
			}
			param.put(pname, request.getParameter(pname));
		}
		en = request.getHeaderNames();
		while(en.hasMoreElements()){
			pname = en.nextElement();
			header.put(pname, request.getHeader(pname));
		}
//		DbUtil.executeSqlUpdate("insert into SYS_T_USER_OPERATOR(dt_time,vc_sessionid,vc_method,vc_uri,vc_param,vc_header) values(?,?,?,?,?,?)",
//				new Object[]{new Date(),sessionId,method,uri,param.toString(),header.toString()});
		OperaUtil.writeOptLog(new Date(), sessionId, uri, method, param.toString(), header.toString());
//		JSONObject obj2 = JSONObject.fromObject(param.toString());
        arg2.doFilter(arg0,arg1);
    }
    private long time;
    private boolean stat;
    private int interval=30000;
    private boolean isLogInfo(){
    	long l = System.currentTimeMillis();
    	if(l-time<interval){
    		return stat;
    	}
    	time = l;
    	stat = RegeditUtil.getRegeditBoolValue("SYSTEM_LOG_OP");
    	return stat;
    }
    
	public void destroy() {
        
        
    }

}
